package com.xxc.hs.funActivity.data;

import android.app.Application;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.xxc.hs.bean.DataInfo;
import com.xxc.hs.bean.DataType;
import com.xxc.hs.bean.WaterDataInfo;
import com.xxc.hs.db.DataHelper;
import com.xxc.hs.db.WaterDataHelper;

@SuppressWarnings("ALL")
public class SensorViewModel extends ViewModel {
    private final MutableLiveData<List<DataInfo>> tempData = new MutableLiveData<>();
    private final MutableLiveData<List<DataInfo>> humData = new MutableLiveData<>();
    private final MutableLiveData<List<DataInfo>> smokeData = new MutableLiveData<>();
    private final MutableLiveData<List<DataInfo>> fireData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> waterLevelData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> phData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> turbidityData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> dissolvedData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> yuLvData = new MutableLiveData<>();
    private final MutableLiveData<List<WaterDataInfo>> waterTempData = new MutableLiveData<>();
    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    private final DataHelper dataHelper;
    private final WaterDataHelper waterDataHelper;
    private int type = 0;

    public SensorViewModel(@NonNull Application application, int type) {
        super();
        this.dataHelper = DataHelper.getInstance(application);
        this.waterDataHelper = WaterDataHelper.getInstance(application);
        this.type = type;
        loadAllData();
    }

    // 加载所有类型数据
    private void loadAllData() {
        new Thread(() -> {
            tempData.postValue(dataHelper.getTemperatureData(this.type));
            humData.postValue(dataHelper.getHumidityData(this.type));
            smokeData.postValue(dataHelper.getSmokeData(this.type));
            fireData.postValue(dataHelper.getFireData(this.type));

            waterLevelData.postValue(waterDataHelper.getWaterLevelData(this.type));
            phData.postValue(waterDataHelper.getPHData(this.type));
            turbidityData.postValue(waterDataHelper.getTurbidityData(this.type));
            dissolvedData.postValue(waterDataHelper.getDissolvedData(this.type));
            yuLvData.postValue(waterDataHelper.getYuLvData(this.type));
            waterTempData.postValue(waterDataHelper.getWaterTempData(this.type));
        }).start();
    }

    // 刷新指定类型数据
    public void refreshData(DataType dataType) {
        new Thread(() -> {
            switch (dataType) {
                case TEMPERATURE:
                    tempData.postValue(dataHelper.getTemperatureData(this.type));
                    break;
                case HUMIDITY:
                    humData.postValue(dataHelper.getHumidityData(this.type));
                    break;
                case SMOKE:
                    smokeData.postValue(dataHelper.getSmokeData(this.type));
                    break;
                case FIRE:
                    fireData.postValue(dataHelper.getFireData(this.type));
                    break;
                case WATERLEVEL:
                    waterLevelData.postValue(waterDataHelper.getWaterLevelData(this.type));
                    break;
                case PH:
                    phData.postValue(waterDataHelper.getPHData(this.type));
                    break;
                case TURBIDITY:
                    turbidityData.postValue(waterDataHelper.getTurbidityData(this.type));
                    break;
                case DISSOLVED:
                    dissolvedData.postValue(waterDataHelper.getDissolvedData(this.type));
                    break;
                case YULV:
                    yuLvData.postValue(waterDataHelper.getYuLvData(this.type));
                    break;
                case WATERTEMP:
                    waterTempData.postValue(waterDataHelper.getWaterTempData(this.type));
                    break;
            }
        }).start();
    }

    public LiveData<List<DataInfo>> getTempData() {
        return tempData;
    }

    public LiveData<List<DataInfo>> getHumData() {
        return humData;
    }

    public LiveData<List<DataInfo>> getSmokeData() {
        return smokeData;
    }

    public LiveData<List<DataInfo>> getFireData() {
        return fireData;
    }

    public LiveData<List<WaterDataInfo>> getWaterLevelData() {
        return waterLevelData;
    }

    public LiveData<List<WaterDataInfo>> getPHData() {
        return phData;
    }

    public LiveData<List<WaterDataInfo>> getTurbidityData() {
        return turbidityData;
    }

    public LiveData<List<WaterDataInfo>> getDissolvedData() {
        return dissolvedData;
    }

    public LiveData<List<WaterDataInfo>> getYuLvData() {
        return yuLvData;
    }

    public LiveData<List<WaterDataInfo>> getWaterTempData() {
        return waterTempData;
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        executor.shutdown();
    }

}

